//
//  KInputTextField.swift
//  koolcar
//
//  Created by xiantian on 2023/9/15.
//

import UIKit

class KInputTextField: UIView {
    
    public var borderColor: CGColor? {
        didSet {
            self.bkgView.layer.borderColor = borderColor
        }
    }
    
    //用来装载leftView, rightView textField
    private var bkgView: UIView = {
        let tempView = UIView()
        tempView.layer.borderWidth = 1
        tempView.layer.borderColor = Theme.tfBorderColor.cgColor
        tempView.layer.cornerRadius = 8
        tempView.backgroundColor = UIColor.white
        return tempView
    }()
    
    private var warnTipLab: UILabel = {
        let label = UILabel()
        label.textColor = Theme.redColor
        label.backgroundColor = UIColor.clear
        label.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        label.numberOfLines = 0
        label.lineBreakMode = .byTruncatingTail
        label.textAlignment = .left
        return label
    }()
    
    var textField: UITextField = {
        let tempView = UITextField()
        tempView.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempView.textColor = Theme.titleLabColor
        tempView.leftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 8, height: 8))
        tempView.clearButtonMode = .whileEditing
        let left = UIView.init(frame: .init(x: 0, y: 0, width: 6, height: 6))
        tempView.leftView = left
        tempView.leftViewMode = .always
        return tempView
    }()
    
    var rightView: UIView? {
        didSet {
            if let right = self.rightView {
                self.bkgView.addSubview(right)
                right.snp.remakeConstraints { make in
                    make.right.equalTo(0)
                    make.top.equalTo(0)
                    make.height.equalTo(right.frame.size.height)
                    make.width.equalTo(right.frame.size.width)
                }
                
                self.textField.snp.updateConstraints { make in
                    make.right.equalTo(-right.frame.size.width - 5).priority(.high)
                }
            }
            self.updateConstraints()
        }
    }
    var leftView: UIView? {
        didSet {
            if let left = self.leftView {
                self.bkgView.addSubview(left)
                left.snp.remakeConstraints { make in
                    make.left.equalTo(0)
                    make.top.equalTo(0)
                    make.height.equalTo(left.frame.size.height)
                    make.width.equalTo(left.frame.size.width)
                }
                
                self.textField.snp.updateConstraints { make in
                    make.left.equalTo(left.frame.size.width).priority(.high)
                }
            }
            self.updateConstraints()
        }
    }
    
    var placeholder: String = "" {
        didSet {
            self.textField.setExtPlaceHolder(placeHolder: self.placeholder, aColor: Theme.placeholderColor)
            self.layoutSubviews()
        }
    }
    var warnText: String = "" {
        didSet {
            if warnText == "" {
                self.borderColor = Theme.tfBorderColor.cgColor
                self.warnTipLab.text = warnText
                self.updateConstraints()
            }else if warnText.removeAllSapce == "" {
                self.borderColor = Theme.tfBorderRedColor.cgColor
            }else{
                self.borderColor = Theme.tfBorderRedColor.cgColor
                self.warnTipLab.text = warnText
                self.updateConstraints()
            }
            
        }
    }
    var keyboardType: UIKeyboardType? {
        didSet {
            self.textField.keyboardType = self.keyboardType ?? .default
        }
    }
    var returnKeyType: UIReturnKeyType? {
        didSet {
            self.textField.returnKeyType = self.returnKeyType ?? .default
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
        self.addSnpLayout()
    }
    
    private func setupUI(){
        bkgView.addSubview(textField)
        self.addSubview(bkgView)
        self.addSubview(warnTipLab)
        self.addSnpLayout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
        }
        
        textField.snp.makeConstraints { make in
            make.left.equalTo(0).priority(.high)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
            make.right.equalTo(0).priority(.high)
            make.height.greaterThanOrEqualTo(54)
        }
        
        warnTipLab.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(bkgView.snp.bottom).offset(4).priority(.high)
            make.bottom.equalTo(0)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
}
